//
//  DCWBaseViewController.m
//  Meng800
//
//  Created by 姚宗超 on 14/7/19.
//  Copyright (c) 2014年 点财网. All rights reserved.
//

#import "MPBaseViewController.h"
#import "UIColor+HexColors.h"

@interface MPBaseViewController ()
@end

@implementation MPBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 解决iOS 7下，view都超过导航栏的问题
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
    }
    
    self.view.backgroundColor = [UIColor colorWithHexString:kContentBgColor];
    
    // 返回按钮
    NSInteger x = [self.navigationController.viewControllers count];
    if (x > 1) {// 多余一级的时候，再创建返回按钮
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        leftButton.frame = CGRectMake(0, 0, 25, 25);
        [leftButton setBackgroundImage:[UIImage imageForKey:@"nav_back_icon"] forState:UIControlStateNormal];
        [leftButton addTarget:self action:@selector(backNavItemPressed) forControlEvents:UIControlEventTouchUpInside];

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        self.navigationItem.hidesBackButton = YES;
    }
    
    [self registerForKeyboardNotifications];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - keyboard avoid
- (CGPoint)absPoint:(UIView *)view
{
    CGPoint ret = CGPointMake(view.frame.origin.x, view.frame.origin.y);
    if ([view superview] != nil)
    {
        CGPoint addPoint = [self absPoint:[view superview]];
        ret = CGPointMake(ret.x + addPoint.x, ret.y + addPoint.y);
    }
    return ret;
}

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)notification
{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    //UIKeyboardFrameEndUserInfoKey: better when conversion candidates appear
    
    CGPoint partsAbsOrigin = [self absPoint:_showParts];
    CGPoint partsAbsBottomPoint = (CGPoint){ partsAbsOrigin.x, partsAbsOrigin.y + _showParts.frame.size.height };
    
    float overlapValue = partsAbsBottomPoint.y - ([[UIApplication sharedApplication] keyWindow].frame.size.height - keyboardSize.height) + 10.0f;
    
    if (overlapValue > 0){
        CGPoint scrollPoint = CGPointMake(0.0, overlapValue);
        [_rootScrollView setContentSize:CGSizeMake(_rootScrollView.contentSize.width, _rootScrollView.contentSize.height + keyboardSize.height + 10.0f)];
        [_rootScrollView setContentOffset:scrollPoint animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    CGSize keyboardSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    [_rootScrollView setContentSize:CGSizeMake(_rootScrollView.contentSize.width, _rootScrollView.contentSize.height - keyboardSize.height - 10.0f)];
    [_rootScrollView setContentOffset:CGPointZero animated:YES];
}


#pragma mark - 返回按钮

- (void)backNavItemPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - property

@end
